var roleArr = [{ value: "", display: "不限" }]; // 角色

$(function() {

  getRoleList(); // 获取角色列表

  var listData = {
    "initData": { //默认参数配置
      "page": "1",
      "pageSize": "10"
    },
    "url": site.apiUrl + "/accountManager/selectMedicalAdminInfoInfoPage", // 数据请求url
    "type": "get",
    // "checkname": "datachk",
    "col": [ //数据列
      { "field": "userName", "display": "账号姓名" },
      { "field": "telephone", "display": "登陆账号" },
      { "field": "roleName", "display": "角色类型" },
      { "field": "hospitalName", "display": "所属机构" },
      { "field": "createUser", "display": "添加人" },
        {
          "field": "status",
          "display": "状态",
          "show": [
            { "value": "0", "html": "<span class='red'>禁止登陆</span>" },
            { "value": "1", "html": "<span class='green'>允许登陆</span>" }
          ]
        },
      { "field": "lastLoginTime", "display": "上次登陆时间", "fun": "changeDate", "width": "2" }
    ],
    "option": [ // 操作按钮配置
      { "icon": "edit", "tip": "编辑", "url": "add.html?id={{id}}&type=2" },
      { "icon": "del", "tip": "删除", "fun": "deleteBrand('{{id}}')" }
    ]
  }
  $("#adminList").getList(listData);

  //批量删除
  $("#batchDelBtn").click(function() {
    var c = "";
    $("input[name=datachk]:checked").each(function() {
      c += $(this).val() + ',';
    });
    if (c != null && c != '') {
      deleteBrand(c);
    } else {
      ly.toast("请选择删除项");
    }
  });

});

// 获取角色列表
function getRoleList() {
  roleArr = [{ value: "", display: "不限" }];
  request({
    url: site.apiUrl + '/roleManager/selectRoleInfoList',
    type: "get",
    data: {
      page: 1,
      pageSize: 5000
    },
    callback: function(f) {
      if (f.code == "success") {
        var forData = f.result.list;
        if (forData.length > 0) {
          for (var i = 0; i < forData.length; i++) {
            roleArr.push({
              value: forData[i].id,
              display: forData[i].roleName
            })
          }
        } else {
          roleArr.push({value: "", display: "暂无角色"})
        }
        $("#roleId").getSelect({
          asName: "roleId",
          type: "select",
          list: roleArr
        });
      }
    }
  })
}

// 时间戳转换时间
function changeDate(v, o, d) {
  var baseDateTime;
  var timestamp = new Date(d.lastLoginTime);
  baseDateTime = timestamp.toLocaleDateString().replace(/\//g, "-") + " " + timestamp.toTimeString().substr(0, 8)
  if (d.lastLoginTime != null && d.lastLoginTime != '') {
    $(o).html(baseDateTime)
  }
}

//删除
function deleteBrand(id) {
  ly.confirm("确定删除吗？", function(r) {
    var url = site.apiUrl + "/accountManager/deleteAdminAccount";
    request({
      url: url,
      data: { "id": id },
      type: "get",
      callback: function(f) {
        if (f.code == 200) {
          ly.toast("删除成功");
          $("#adminList").getList.refresh(); //刷新当前分页
        } else {
          ly.toast(f.desc || f.msg);
        }
      }
    })
  })
}
